package com.sh.data.engine.domain.taskalarm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sh.data.engine.domain.base.model.PageResult;
import com.sh.data.engine.domain.taskalarm.model.domain.AlarmHistoryDomain;
import com.sh.data.engine.domain.taskalarm.model.param.AlarmHistoryParam;
import com.sh.data.engine.domain.taskalarm.service.TaskAlarmHisService;
import com.sh.data.engine.domain.util.ConvertUtil;
import com.sh.data.engine.repository.dao.taskalarm.TaskAlarmHistoryMapper;
import com.sh.data.engine.repository.dao.taskalarm.entity.TaskAlarmHistoryEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * @author tinglan.ys
 * @description:
 * @date 2022/4/28 下午5:15
 */
@Service
public class TaskAlarmHisServiceImpl
    extends ServiceImpl<TaskAlarmHistoryMapper, TaskAlarmHistoryEntity>
    implements TaskAlarmHisService {

    @Override
    public PageResult<AlarmHistoryDomain> getAlarmHistoryPageList(
        AlarmHistoryParam request, Long projectId) {

        Integer pageNum = request.getPageNum();
        Integer pageSize = request.getPageSize();

        Page<TaskAlarmHistoryEntity> page =
            this.page(
                new Page<>(pageNum, pageSize),
                new LambdaQueryWrapper<TaskAlarmHistoryEntity>()
                    .eq(TaskAlarmHistoryEntity::getProjectId, projectId)
                    .like(
                        StringUtils.isNotBlank(request.getUsername()),
                        TaskAlarmHistoryEntity::getReceiverUsername,
                        request.getUsername())
                    .eq(
                        request.getReceiveType() != null,
                        TaskAlarmHistoryEntity::getReceiveType,
                        request.getReceiveType())
                    .eq(
                        request.getSubjectType() != null,
                        TaskAlarmHistoryEntity::getSubjectType,
                        request.getSubjectType())
                    .orderByDesc(TaskAlarmHistoryEntity::getCreateTime));

        PageResult<TaskAlarmHistoryEntity> taskAlarmHistoryEntityPageResult =
            new PageResult<>(page.getTotal(), page.getRecords(), pageNum, pageSize);

        PageResult<AlarmHistoryDomain> alarmHistoryDomainPageResult =
            ConvertUtil.convertPageInfo(taskAlarmHistoryEntityPageResult, AlarmHistoryDomain.class);

        return alarmHistoryDomainPageResult;
    }

    @Override
    public Boolean saveTaskAlarmHistory(TaskAlarmHistoryEntity taskAlarmHistoryEntity) {
        this.save(taskAlarmHistoryEntity);

        return true;
    }


}
